<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 列表管理
 */
namespace app\finance\model;

use  app\finance\model\Model;
use  app\finance\model\Common;

class Examine extends Model{
  
    private $user = 'user_user'; // 用户表
    private $account = 'user_account'; // 账户表
    private $assets = 'user_assets'; // 资产表
    private $apply = 'user_apply'; // 兑换表
    private $loan = 'user_loan'; // 贷款表
    private $reloan = 'user_reloan'; //还款表
    private $withdraw = 'user_withdraw'; // 提现表
    private $recharge = 'user_recharge'; // 提现表
    

    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success']; 


    /**
     * 提现审核
     * @param array $draw 
     * @param array $where 
     */
    public function dexamine($data=[],$operator=[]){
        $id=$data['id'];
        //保存
        $data  = [
            'aid'=>$data['aid'],
            'money'=>$data['money'],
            'fee'=>$data['fee'],
            'integra'=>$data['integra'],
            'payment'=>$data['payment'],
            'notes'=>$data['notes'],
            'status'=>$data['status'],
            'updatetime'=>date('Y-m-d H:i:s')
        ];
        Model::startTrans();
        try {
            $user=(new Common())->getAccount('aid='.$data['aid'],'amount,integra,cash,username,fee');
            switch ($data['status']) {
                case 3:
                    $updata=[
                        'cash'=>$user['cash']+$data['money']+$data['integra'],
                        'fee'=>$user['fee']+$data['fee']
                    ];
                    (new Common())->updateAccount('aid='.$data['aid'],$updata);
                    logrecord(
                        '记录id'.$id.';账户'.$user['username'].';金额：'.$user['amount'].';赠送:'.$user['integra'].';交易金额：'.$data['money'].';手续费：'.$data['fee'].';审核状态：通过',
                        $operator,
                        'withdraw/'
                    );
                    break;
                case 2:
                    $updata=[
                        'amount'=>($user['amount']+$data['money']-$data['integra']),
                        'integra'=>($user['integra']+$data['integra']),
                    ];
                    logrecord(
                        '记录id'.$id.';账户'.$user['username'].';金额：'.$user['amount'].';赠送:'.$user['integra'].';交易金额：'.$data['money'].';手续费：'.$data['fee'].';审核状态：退回',
                        $operator,
                        'withdraw/'
                    );
                    (new Common())->updateAccount('aid='.$data['aid'],$updata);
                    break;
                default:
                    break;
            }
            unset($data['aid'],$data['money'],$data['fee']);
            Model::name($this->withdraw)->where('id=' . $id)->update($data);
            Model::commit();
            return true;
        } catch (Exception $e) {
            Model::rollback();
            return false;
        }
        return false;
    }

    /**
     * 充值審核
     * @param array $examine 
     * @param array $where 
    */ 
    public function examine($data,$operator=[]){
        $id=$data['id'];
        //保存
        $data  = [
            'aid'=>$data['aid'],
            'type'=>$data['type'],
            'reintegra'=>isset($data['reintegra'])?$data['reintegra']:0,
            'remoney'=>$data['remoney'],
            'notes'=>$data['notes'],
            'status'=>$data['status'],
            'updatetime'=>date('Y-m-d H:i:s')
        ];
        $amount=(new Common())->getAccount('aid='.$data['aid'],'amount,giftamount,integra,username');
        switch ($data['status']) {
            case 3:
                Model::startTrans();
                try {
                    switch ($data['type']) {
                        case 1:
                            (new Common())->updateAccount('aid='.$data['aid'],['amount'=>($amount['amount']+$data['remoney']),'integra'=>($amount['integra']+$data['reintegra'])]);
                            break;
                        case 2:
                            (new Common())->updateAccount('aid='.$data['aid'],['giftamount'=>($amount['giftamount']+$data['remoney'])]);
                            break;
                    }
                    logrecord(
                        '记录id'.$id.';账户'.$amount['username'].'; 账户金额：'.($amount['amount']+$amount['integra']).';赠送:'.$data['reintegra'].';交易金额：'.$data['remoney'].';审核：通过',
                        $operator,
                        'recharge/'
                    );
                    unset($data['aid'],$data['type']);
                    Model::name($this->recharge)->where('id=' . $id)->update($data);
                    Model::commit();
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                return true;
            default:
                if($data['status']==2){
                    $msg='退回'; 
                }else{
                    $msg='无审核'; 
                }
                logrecord(
                    '记录id'.$id.';账户'.$amount['username'].'; 账户金额：'.($amount['amount']+$amount['integra']).';赠送:'.$data['reintegra'].';交易金额：'.$data['remoney'].';审核：'.$msg,
                    $operator,
                    'recharge/'
                );
                Model::startTrans();
                try {
                    unset($data['aid'],$data['type'],$data['reintegra']);
                    Model::name($this->recharge)->where('id=' . $id)->update($data);
                    Model::commit();
                } catch (Exception $e) {
                    Model::rollback();
                    return false;
                }
                return true;
        }
        return false;
    }

}
